﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;

namespace DevelopTool.Forms.TextProcessForms
{
    /// <summary>
    /// 通用唯一识别码 GUID生成
    /// </summary>
    public partial class GenerateGUIDForm : BaseUIPage
    {
        public GenerateGUIDForm()
        {
            InitializeComponent();
        }

        private void GenerateUUIDForm_Load(object sender, EventArgs e)
        {
            GenerateGuid();
        }


        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            GenerateGuid();
        }


        private void GenerateGuid()
        {
            #region 生成数量大于1时，必须要选择一个记录值
            if (this.upDownGenerateCount.Value > 1)
            {
                List<UICheckBox> checkBoxList = this.GetControls<UICheckBox>(true);
                if (!checkBoxList.Any(it => it.Name.Contains("chbRecord") && it.Checked == true))
                {
                    this.ShowWarningNotifier("生成数量大于1时，请至少选择一个记录值~");
                    return;
                }
            }
            #endregion


            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < this.upDownGenerateCount.Value; i++)
            {
                Guid guid = Guid.NewGuid();
                this.txtGuidD.Text = UpperStr(guid.ToString("D"));
                if (this.chbRecordD.Checked) sb.AppendLine(this.txtGuidD.Text);

                this.txtGuidN.Text = UpperStr(guid.ToString("N"));
                if (this.chbRecordN.Checked) sb.AppendLine(this.txtGuidN.Text);

                this.txtGuidB.Text = UpperStr(guid.ToString("B"));
                if (this.chbRecordB.Checked) sb.AppendLine(this.txtGuidB.Text);

                this.txtGuidP.Text = UpperStr(guid.ToString("P"));
                if (this.chbRecordP.Checked) sb.AppendLine(this.txtGuidP.Text);

                this.txtGuidX.Text = UpperStr(guid.ToString("X"));
                if (this.chbRecordX.Checked) sb.AppendLine(this.txtGuidX.Text);
            }

            this.codeEditorControl.ContentText += sb.ToString();

            this.ShowSuccessTip(this.btnGenerate, "已生成" + this.upDownGenerateCount.Value);
        }



        /// <summary>
        /// 大写字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string UpperStr(string str)
        {
            return chbUpper.Checked ? str.ToUpper() : str;
        }

        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtGuid_ButtonClick(object sender, EventArgs e)
        {
            UITextBox textBox = sender as UITextBox;
            Clipboard.SetText(textBox.Text);
            this.ShowSuccessTip(textBox, "已复制");
        }
    }
}
